package com.lj.controller;

import com.github.pagehelper.PageInfo;
import com.lj.pojo.Departmentlj;
import com.lj.pojo.Directorlj;
import com.lj.service.DepartmentService;
import com.lj.service.DirectorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/department")
public class DepartmentAction {
    public static final int PAGE_SIZE=5;
    @Autowired
    DepartmentService departmentService;
    @RequestMapping("/selectDepartment")
    public String selectDepartment(HttpServletRequest request){
        //得到第一页数据
        PageInfo info=departmentService.selectDepartment(1,PAGE_SIZE);
        request.setAttribute("info",info);
        return "admin/departmentView";
    }
    //ajax分页翻页处理
    @ResponseBody
    @RequestMapping("/ajaxSplit")
    public void ajaxSplit(int page, HttpSession session){
        //取得当前page参数页面的数据
        PageInfo info=departmentService.selectDepartment(page,PAGE_SIZE);
        session.setAttribute("info",info);
    }
    @RequestMapping(value = "/insert")
    public String insert(Departmentlj departmentlj){
        if(departmentlj.getManagerId().equals("null")){
            departmentlj.setManagerId(null);
        }
        int num=-1;
        num=departmentService.insertDepartment(departmentlj);
        return "redirect:/department/selectDepartment.action";
    }
    @RequestMapping("/delete")
    public String delete(String departmentName){
        int nums=-1;
        nums=departmentService.delete(departmentName);
        return"forward:/department/selectDepartment.action";
    }
    @ResponseBody
    @RequestMapping("/getDepartmentById")
    public Departmentlj getDirectorById(String departmentName, HttpServletRequest request ){
        Departmentlj departmentlj=departmentService.getById(departmentName);
        return departmentlj;
    }
    @RequestMapping("/update")
    public String update(Departmentlj departmentlj){
        if(departmentlj.getManagerId().equals("null")){
            departmentlj.setManagerId(null);
        }
        int nums= departmentService.update(departmentlj);
        return "redirect:/department/selectDepartment.action";
    }
}
